//
//  singleNextPageViewController.m
//  mintHome
//
//  Created by 朱彦名 on 16/6/21.
//  Copyright © 2016年 朱彦名. All rights reserved.
//

#import "singleNextPageViewController.h"
#import "CollectionWebViewModel.h"
@interface singleNextPageViewController ()<UIWebViewDelegate>

@end

@implementation singleNextPageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"商品详情";
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"xihuan"] style:UIBarButtonItemStylePlain target:self action:@selector(handleTouchSou)];
    
    
    UIWebView *webview = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    [MBProgressHUDManager showHUDtoView:webview string:@"加载中"];
    webview.delegate = self;
    [self.view addSubview:webview];
    
    [webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.url2]]];
    
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
    
    
[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.getElementsByClassName('purchase-content')[0].style.display = 'none'"];
[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.getElementsByClassName('header-sm')[0].style.display = 'none'"];
[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.getElementsByClassName('content-right')[0].style.display = 'none'"];
[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.getElementsByClassName('download-content')[0].style.display = 'none'"];
[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.getElementsByClassName('header')[0].style.display = 'none'"];
    
    [MBProgressHUDManager removeHUD];
    
}
- (void)handleTouchSou{
    
    
    CollectionWebViewModel *p = [[CollectionWebViewModel alloc]init];
    p.title = _title1;
    p.urlStr = _url2;
    
    // 1. 获取路径
    // 写本地的文件后缀没有任何影响
    NSString *collectionPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"collectionWebView.txt"];
    // 先获取
    NSMutableArray *arr = [NSKeyedUnarchiver unarchiveObjectWithFile:collectionPath];
    // 数组存在 直接添加
    if (arr) {
        for (CollectionWebViewModel *p in arr) {
            if ([p.title isEqualToString:self.title1]) {
                UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"已经收藏" message:@"请到我的收藏中查看" preferredStyle:UIAlertControllerStyleAlert];
                
                
                [self presentViewController:alert animated:YES completion:^{
                    
                }];
                [self performSelector:@selector(diss) withObject:self afterDelay:1];
                return;
            }
        }
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:@"收藏成功" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
        
        [alert addAction:sure];
        [self presentViewController:alert animated:YES completion:nil];
        
        [arr addObject:p];
        
    }else{
        arr = [NSMutableArray array];
        [arr addObject:p];
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:@"收藏成功" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
        [alert addAction:sure];
        
        [self presentViewController:alert animated:YES completion:nil];
    }
    // 归档
    [NSKeyedArchiver archiveRootObject:arr toFile:collectionPath];

}
- (void)diss
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}
@end
